pub mod event_t {
    use wasm_bindgen::{prelude::Closure, JsCast};
    use web_sys::HtmlElement;
    use yew::{
        events::Event,
        html,
        Component, Context, Html, NodeRef
    };

    pub struct EventSelfComp {
        my_div: NodeRef,
        custard_listener: Option<Closure<dyn Fn(Event)>>,
    }

    pub enum Msg {
        // custom Message
        Custard,
    }

    impl Component for EventSelfComp {
        type Message = Msg;
        type Properties = ();

        fn create(_ctx: &Context<Self>) -> Self {
            Self {
                my_div: NodeRef::default(),
                custard_listener: None,
            }
        }
        fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
            match msg {
                Msg::Custard => {
                    weblog::console_log!("Wow Wow");
                    true
                }
            }
        }
        fn view(&self, _ctx: &Context<Self>) -> Html {
            html! {
                <div ref={self.my_div.clone()} id="my_div"></div>
            }
        }
        fn rendered(&mut self, ctx: &Context<Self>, first_render: bool) {
            if !first_render {
                return;
            }
            if let Some(element) = self.my_div.cast::<HtmlElement>() {
                // Normally create a callback
                let oncustard = ctx.link().callback(|_: Event| Msg::Custard);
                // create closure from a Box<dyn Fn> --- static
                let listener = 
                    Closure::<dyn Fn(Event)>::wrap(
                        Box::new(move |e: Event| oncustard.emit(e))
                    );
                element.add_event_listener_with_callback("custard", listener.as_ref().unchecked_ref()).unwrap();

                // save the listener in the component
                self.custard_listener = Some(listener);
            }
        }
        fn destroy(&mut self, _ctx: &Context<Self>) {
            if let (Some(element), Some(listener)) = (
                self.my_div.cast::<HtmlElement>(), self.custard_listener.take(),) {
                    element.remove_event_listener_with_callback("custard", listener.as_ref().unchecked_ref()).unwrap();
                } 
        }
    }
}